using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.Providers
{
    public class AcmsSiteMapProvider : System.Web.SiteMapProvider
    {
        private string menuName;
        private string rootText;
        private string rootUrl;
        private string rootParentUrl;
        private int siteId;
        private int menuId;

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection attributes)
        {
            base.Initialize(name, attributes);

            menuName = attributes["MenuName"];
            rootText = attributes["RootText"]; 
            rootUrl = attributes["RootUrl"];
            rootParentUrl = attributes["RootParentUrl"];
            siteId = int.Parse(attributes["SiteId"]);

            if (!string.IsNullOrEmpty(menuName))
            {
                Bll.Menu tmp = Bll.Menu.GetByNameAndSiteId(menuName, siteId);
                if (tmp != null)
                {
                    menuId = tmp.Id;
                }
                else
                {
                    throw new ApplicationException(string.Format("Could not find menu '{0}'", menuName));
                }
            }
        }



        public override System.Web.SiteMapNode FindSiteMapNode(string rawUrl)
        {
            return null;
        }

        public override System.Web.SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode node)
        {
            if (node.Key == "RootText")
            {
                List<Bll.Post> posts = null;
                if (menuId > 0)
                {
                    posts = Bll.Post.GetAllBySiteIdMenuIdAndParentPath(siteId, menuId, rootParentUrl);
                }
                else
                {
                    posts = Bll.Post.GetAllBySiteIdAndParentPath(siteId, rootParentUrl);
                }
                return ConvertPosts(posts);
            }
            else
            {
                int nodeId = int.Parse(node.Key);
                List<Bll.Post> posts = null;
                if (menuId > 0)
                {
                    posts = Bll.Post.GetAllByMenuIdAndParentId(menuId, nodeId);
                }
                else
                {
                    posts = Bll.Post.GetAllByParentId(nodeId);
                }

                return ConvertPosts(posts);

            }
        }

        public override System.Web.SiteMapNode GetParentNode(System.Web.SiteMapNode node)
        {
            throw new Exception("The method or operation is not implemented.");
        }

 
        protected override System.Web.SiteMapNode GetRootNodeCore()
        {
            if (!string.IsNullOrEmpty(rootText))
            {
                System.Web.SiteMapNode tmp = new System.Web.SiteMapNode(this, "RootText", null, rootText);
                return tmp;
            }
            else if (!string.IsNullOrEmpty(rootUrl))
            {
                if (menuId > 0)
                {
                    return ConvertPost(Bll.Post.GetBySiteIdMenuIdAndUrl(siteId, menuId, rootUrl));
                }
                else
                {
                    return ConvertPost(Bll.Post.GetBySiteIdAndUrl(siteId, rootUrl));
                }
            }
            else
            {
                throw new ApplicationException("Either Root Text and RootParentUrl must be set or Root Url must be set");
            }
        }

        private System.Web.SiteMapNode ConvertPost(Bll.Post post)
        {
            if (post != null)
            {
                System.Web.SiteMapNode tmp = new System.Web.SiteMapNode(this, post.Id.ToString());
                tmp.Title = post.DisplayName;
                tmp.Url = post.Url;
                return tmp;
            }
            else
            {
                return null;
            }
        }

        private System.Web.SiteMapNodeCollection ConvertPosts(List<Bll.Post> posts)
        {

            System.Web.SiteMapNodeCollection tmp = new System.Web.SiteMapNodeCollection();
            if (posts != null)
            {
                foreach (Bll.Post p in posts)
                {
                    tmp.Add(ConvertPost(p));
                }
            }
            return tmp;
        }
    }
}
